/**
 * Created by zhoulin on 2019/4/3.
 */
var interval = [];
var deptId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.deptId);
var app = new Vue({
    el: '#app',
    data: {
        permissions: ['companyApply:deal','permission:list'],
        identify:"@",//账号标识
        passText:"随机生成",//密码生成按钮名称
        serviceVersionTypes:false,
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if(empty(token)){
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon:5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200)

            return ;
        }

        that.getDictionary();
        that.getServiceVersionTypes();

        if(!empty(aGET.id)){
            that.init();
            //that.getPermissionZTree();
        }

    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        //解决修改时候数据不及时更新问题
        init:function () {
            var that = this;
            if(Boolean(that.serviceVersionTypes)){
                for (var itme in interval){
                    window.clearInterval(interval[itme]);
                }
                that.getInfoById(aGET.id);
            }else {
                interval.push(
                    window.setInterval(function () {
                        that.init();
                    },1000))
            }
        },
        getInfoById:function (id) {
            var that = this;
            Get(that,_HOST+_API.business_companyApply_getInfoById+id)
                .then(function (response) {
                    // 响应成功回调
                    //console.log(response.data)
                    if(response){
                        if(response.success){
                            var list = response.data
                            for (var item in list) {
                                if("type" == item){
                                    continue ;
                                }
                                $("#Form input[name='" + item + "']").val(list[item]);
                            }
                            $("#serviceVersionTypes input[name='serviceVersionType'][value='" + list['type']+ "']").iCheck('check');
                            $("#Form input[name='username']").val(list['shortName']);
                            $("#Form input[name='nickname']").val(list['shortName']);

                            $("#Form").data('bootstrapValidator').destroy();
                            $('#Form').data('bootstrapValidator', null);
                            Validator();
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        //获取字典表配置数据
        getDictionary:function () {
            var that = this;
            var params = "?keyTable=_IDENTIFY"
            Get(that,_HOST+_API.common_dic_dicList+params)
                .then(function (response) {
                    // 响应成功回调
                    //console.log(response)
                    if(response){
                        if(response.success){
                            var list = response.list
                            var domOption = "";
                            for (var item in list) {
                                if("accountIdentify" == list[item].keyS){
                                    that.identify = list[item].keyValue;
                                }
                            }
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        //获取权限树
        getPermissionZTree:function () {
            var that = this;
            //获取当前登录人拥有的角色
            Get(that,_HOST+_API.admin_sysPermission_list+"1",that.permissions[1])
                .then(function (response) {
                    // 响应成功回调
                    //console.log(response)
                    if(response){
                        if(response.success){
                            //console.log(response.list)
                            //树形初始化
                            var setting = {
                                check: {
                                    enable: true,
                                },
                                data: {
                                    simpleData: {
                                        enable: true,
                                        idKey: "id",
                                        pIdKey: "pid",
                                        rootPId: 0
                                    }
                                },
                                callback: {
                                    onClick: function (e, treeId, treeNode, clickFlag) {
                                        $.fn.zTree.checkNode(treeNode, !treeNode.checked, true);
                                    }
                                },

                            };
                            $.fn.zTree.init($("#treeDemo"), setting, response.list);
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        //获取所有服务类别
        getServiceVersionTypes:function () {
            var that = this;
            Get(that,_HOST+_API.business_serviceVersionType_listAll)
                .then(function (response) {
                    // 响应成功回调
                    if(response){
                        var html = "";
                        for (var item in response.list) {
                            html = html + '<label class="radio-inline i-checks"><input type="radio" name="serviceVersionType" value="'+response.list[item].id+'"> '+response.list[item].name+'</label>'
                        }

                        $("#Form #serviceVersionTypes").append(html);
                        $("#serviceVersionTypes .i-checks").iCheck({checkboxClass: "icheckbox_square-green",radioClass: "iradio_square-green",});
                        that.serviceVersionTypes = true;
                    }else {
                        layer.msg(response.msg, {time: 1500, icon:5});
                    }
                })
        },
        save:function () {
            var that = this;
           /* //选中角色
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            nodes = zTree.getCheckedNodes(true);
            if (nodes.length <= 0) {
                parent.layer.msg('请至少选中一个权限', {time: 1500, icon: 5});
                return;
            }

            //得到选中节点ID
            var rolePermission = [];
            for (var i = 0; i < nodes.length; i++) {
                rolePermission.push({"rid":0, "pid": nodes[i].id})
            }*/

           if(empty($("#serviceVersionTypes input[name='serviceVersionType']:checked").val())){
               parent.layer.msg('请选择服务套餐类别', {time: 1500, icon: 5});
               return;
           }

            var param = formToJson($("#Form").serialize());
            param.username=param.username+that.identify;
            param.serviceVersionType = $("#serviceVersionTypes input[name='serviceVersionType']:checked").val();
            console.log(param)
           Post(that,_HOST+_API.business_companyApply_deal,param,true,that.permissions[0])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response)
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                    $("#Form").data('bootstrapValidator').destroy();
                    $('#Form').data('bootstrapValidator', null);
                    Validator();
                })
        },
        //随机生成密码
        createPass:function () {
            $("#Form input[name='password']").attr("disabled",false);
            this.passText = "随机生成";
            $("#password").val(RndNum(6));
        },
    },
})

$('#Form').data('bootstrapValidator', null);
Validator();
function Validator() {
    $('#Form').bootstrapValidator({
        fields: {
            username: {
                validators: {
                    notEmpty: {
                        message: '用户名不能为空'
                    },
                    stringLength: {
                        max: 50,
                        message: '用户名长度最多50个字符'
                    },
                }
            },
            nickname: {
                validators: {
                    notEmpty: {
                        message: '昵称不能为空'
                    },
                    stringLength: {
                        max: 1000,
                        message: '昵称长度最多1000个字符'
                    },
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: '密码不能为空'
                    },
                    stringLength: {
                        min: 6,
                        /*max: 30,*/
                        message: '密码长度最少6位数'
                    }
                }
            },
            email: {
                validators: {
                    notEmpty: {
                        message: '邮箱不能为空'
                    },
                    /*emailAddress:{
                        message: '邮箱格式不正确'
                    },*/
                    stringLength: {
                        max: 50,
                        message: '邮箱长度最多50个字符'
                    },
                }
            },
        }
    }).on('success.form.bv', function (e) {
        // Prevent form submission
        e.preventDefault();
       app.save();
    });
}
